package com.liuyang.homework.Util;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//分词工具
public class CTAS {

    private static Set<String> stoplist_dict;//存放停用词
	private static Set<String> seg_dict;//存放分词表
    //正向
	private static Vector<String> FMM2(String  phrase){
        //获取词表最大长度
        int maxlen = 0;
        for (String temp :seg_dict ){
            if (temp.length()> maxlen){
                maxlen = temp.length();
            }
        }
		Vector<String> fmm_list = new Vector<String>();
		int len_phrase = phrase.length();
		int i=0,j=0;
		
		while(i < len_phrase){
			int end = i+maxlen;
			if(end >= len_phrase)
				end = len_phrase;

			String phrase_sub = phrase.substring(i, end);

			for(j = phrase_sub.length(); j >=0; j--){
				if(j == 1)
					break;
				String key =  phrase_sub.substring(0, j);
				if(seg_dict.contains(key)){
					fmm_list.add(key);
					i +=key.length() -1;
					break;
				}
			}
			if(j == 1)
				fmm_list.add(""+phrase_sub.charAt(0));
			i+=1;
		}

		return fmm_list;
	}
	
    //反向
	private static Vector<String> BMM2( String  phrase){
        //获取词表最大长度
		int maxlen = 0;
        for (String temp :seg_dict ){
            if (temp.length()> maxlen){
                maxlen = temp.length();
            }
        }

        Vector<String> bmm_list = new Vector<String>();
		int len_phrase = phrase.length();
		int i=len_phrase,j=0;
		
		while(i > 0){
			int start = i - maxlen;
			if(start < 0)
				start = 0;
			String phrase_sub = phrase.substring(start, i);
			for(j = 0; j < phrase_sub.length(); j++){
				if(j == phrase_sub.length()-1)
					break;
				String key =  phrase_sub.substring(j);
				if(seg_dict.contains(key)){
					bmm_list.insertElementAt(key, 0);
					i -=key.length() -1;
					break;
				}
			}
			if(j == phrase_sub.length() -1)
				bmm_list.insertElementAt(""+phrase_sub.charAt(j), 0);
			i -= 1;
		}
		return bmm_list;
	}
		
    //双向对比
	public static Vector<String> segment( String phrase){
		Vector<String> fmm_list = FMM2(phrase);
		Vector<String> bmm_list = BMM2(phrase);
		if(fmm_list.size() != bmm_list.size()){
			if(fmm_list.size() > bmm_list.size())
				return bmm_list;
			else return fmm_list;
		}else{
			int i ,FSingle = 0, BSingle = 0;
			boolean isSame = true;
			for(i = 0; i < fmm_list.size();  i++){
				if(!fmm_list.get(i).equals(bmm_list.get(i)))
					isSame = false;
				if(fmm_list.get(i).length() ==1)
					FSingle +=1;
				if(bmm_list.get(i).length() ==1)
					BSingle +=1;
			}
			if(isSame)
				return fmm_list;
			else{
				if(BSingle > FSingle){

					for (int j = 0; j < fmm_list.size() ; j++) {
						if (stoplist_dict.contains(fmm_list.get(j))){
							fmm_list.remove(fmm_list.get(j));
							j--;
						}
					}
					return fmm_list;
				}else{
					for (int k = 0; k < bmm_list.size() ; k++) {
						if (stoplist_dict.contains(bmm_list.get(k))){
							bmm_list.remove(bmm_list.get(k));
							k--;
						}
					}
					return bmm_list;
				}
			}
		}
	}

	public static Vector<String> main(Context context,String args){
        //给定的中文文本


		System.out.println("读取文件中...");
				//加载停用词表
				System.out.println("加载停用词表...");

				stoplist_dict = new HashSet<String>();
				String stoplist_line = null;

				BufferedReader stoplist_br;
				try{
					stoplist_br = new BufferedReader( new InputStreamReader( context.getAssets().open("stoplis.txt")));
					while((stoplist_line = stoplist_br.readLine()) != null){
						stoplist_line = stoplist_line.trim();
						if(stoplist_line.isEmpty())
							continue;
						stoplist_dict.add(stoplist_line);
					}
					stoplist_br.close();
				}catch(IOException e){
					e.printStackTrace();
				}

				//加载分词表
				System.out.println("加载分词表..");
				seg_dict = new HashSet<String>();
				String line = null;

				BufferedReader br;
				try{
					br = new BufferedReader( new InputStreamReader( context.getAssets().open("SEGMENTDICT.TXT")));
					while((line = br.readLine()) != null){
						line = line.trim();
						if(line.isEmpty())
							continue;
						seg_dict.add(line);
					}
					br.close();
				}catch(IOException e){
					e.printStackTrace();
				}

				//开始分词，打印结果
				System.out.println("开始分词...");
				//System.out.println(CTAS.segment(text));
				//输出结果
				System.out.println("结果已输出...");

				return CTAS.segment(args);


	}

	public static boolean isChinese(String str) {
		int count = 0;
		String regEx = "[\\u4e00-\\u9fa5]";
		//System.out.println(regEx);
		//System.out.println(str);
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		while (m.find()) {
			for (int i = 0; i <= m.groupCount(); i++) {
				count = count + 1;
			}
		}
		if (count>0){
			return true;
		}else{
			return false;
		}

	}
}
